<?php

namespace App\Traits;

use duncan3dc\Laravel\BladeInstance;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\Core;

use think\Template;

trait LaravelTemplate
{
    private   $view;
    protected $share_data = [];

    /**
     * 输出模板到页面
     *
     * @param string|null $template 模板文件
     * @param array       $vars     模板变量值
     * @param array       $config   额外的渲染配置
     *
     * @author : evalor <master@evalor.cn>
     */
    public function share($vars = [])
    {
        $this->share_data = $vars;
    }

    public function fetch($template = null, $vars = [])
    {
        $res = $this->view->render($template, array_merge($vars, $this->share_data));

        $this->response()->write($res);
    }

    public function load_template(string $path = '/Resources/views', string $cache = '/storage/framework/views')
    {
        return $this->view = new BladeInstance(EASYSWOOLE_ROOT . $path, EASYSWOOLE_ROOT . $cache);
    }
}
